package com.example.tulingtest;



import com.example.model.Apple;

import java.lang.reflect.Proxy;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description:
 * @Project:
 * @CreateDate: Created in 2019/3/5 16:08
 * @Author: zjq
 */
public class SortTest {

    public static void main(String[] args) {

        List<Apple> appleStore = new ArrayList<>();
        appleStore.add(new Apple(1, "black", 13, "nanjing"));
        appleStore.add(new Apple(2, "red", 9, "suzhou"));
        appleStore.add(new Apple(3, "blue", 14, "nantong"));
        appleStore.add(new Apple(4, "pink", 15, "xuzhou"));

        //以前的写法  排序
        System.out.println("以前写法的 排序 weight从小到大 ");
        Collections.sort(appleStore, new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getWeight() - o2.getWeight();
            }
        });
        appleStore.stream().forEach(n -> System.out.println(n));


        /**
         *  .stream().sorted.....collect()：返回新的有序集合，原集合顺序不变
         *    .sort...：返回void，原集合顺序变为有序
         */


        //lamba表达式优化
        System.out.println("lambda表达式（不基于stream） 排序  weight从大到小 ");
        Collections.sort(appleStore, (o1, o2) -> o2.getWeight() - o1.getWeight());
        appleStore.stream().forEach(System.out::println);

        //更简单的方法 基于stream流的方式 排序
        System.out.println("lambda表达式（基于stream） 排序  weight从小到大 ");
        Stream<Apple> sorted = appleStore.stream().sorted((o1, o2) -> o1.getWeight() - o2.getWeight());
        sorted.forEach(System.out::println);

        //过滤 因为数据库成本高  过滤逻辑在代码中写，比如redis的条件查询
        List<Apple> list = new ArrayList<>();
        list.add(new Apple(1, "black", 13, "nanjing"));
        list.add(new Apple(3, "red", 9, "suzhou"));
        list.add(new Apple(2, "blue", 14, "nantong"));
        list.add(new Apple(5, "pink", 15, "xuzhou"));
        list.add(new Apple(4, "red", 15, "xian"));

        System.out.println("lambda条件过滤(且)  加排序");
        Stream<Apple> stream = list.stream().filter(a -> a.getColor().equalsIgnoreCase("red"))
                .filter(a -> a.getOrigin().equalsIgnoreCase("suzhou"))
                .sorted((o1, o2) -> o1.getWeight() - o2.getWeight());
        stream.forEach(System.out::println);

        System.out.println("lambda条件过滤(或)  加排序");
        Stream<Apple> stramor = list.stream().filter((x -> x.getColor().equalsIgnoreCase("pink") ||
                x.getOrigin().equalsIgnoreCase("nanjing")))
                .sorted((o1,o2)->o1.getId()-o2.getId());
        stramor.forEach(System.out::println);
        //key: red  value[Apple{id=2, color='red', weight=9, origin='suzhou'}, Apple{id=4, color='red', weight=15, origin='xian'}]
        System.out.println("lambda  分组  会将相同的一组的东西放入一个k的value里头");
        Map<String, List<Apple>> map = list.stream().collect(Collectors.groupingBy(a -> a.getColor()));  //根据颜色分组
        for(String k:map.keySet()){
            System.out.println("key: "+k+"  value"+map.get(k) );
        }
        System.out.println("lambda  分页");
        Stream<Apple> limit = list.stream().limit(3);
        limit.forEach(System.out::println);


        System.out.println("lambda  map用法（基本类型）");
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
        Stream<String> stringStream = list1.stream().sorted((o1, o2) -> o1 - o2).map(n -> String.valueOf(n) + "qqq0");
        stringStream.forEach(System.out::println);
        
        System.out.println("lambda  map用法（对象）");
//        map(p -> rootPath + "/" + p)
        Stream<String> stringStream1 = list.stream().map(n -> "00100" + n.getOrigin());
        stringStream1.forEach(System.out::println);


        //动态代理 调用接口
        SayHello f = (SayHello) Proxy.newProxyInstance(SortTest.class.getClassLoader()
                , new Class[]{SayHello.class}, (p, m, a) -> {
                    return "zjq";
                });
        f.SayHello();

        System.out.println("====================================");

//        appleStore.stream().sorted((o1, o2) -> o2.getId() - o1.getId() ).forEach(n-> System.out.println(n));

    }

    public interface SayHello {
        void SayHello();
    }


}
